package com.qf.hospital.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageInterceptor;
import com.qf.hospital.dto.SysUserDTO;
import com.qf.hospital.dto.SysUserUpdateDTO;
import com.qf.hospital.entity.SysUser;
import com.qf.hospital.enums.HospitalExceptionEnums;
import com.qf.hospital.service.SysUserService;
import com.qf.hospital.vo.R;
import com.qf.hospital.vo.SysUserVO;
import com.qf.hospital.vo.UserRoleVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ASUS
 * @version V1.0
 * @Project hospital
 * @Package com.qf.hospital.controller
 * @Date 2021/10/18 9:23
 * @description 用户管理功能
 */
@RestController
public class UserController {

    private Logger log = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private SysUserService sysUserService;

    /**
     * 根据条件查询用户信息
     * @param realname
     * @param loginname
     * @param identity
     * @param phone
     * @param address
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/user/selectAllUser")
    public R selectAllUser(String realname, String loginname, String identity, String phone, String address,
                           @RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "10") Integer limit){
        PageInfo<SysUser> pageInfo = sysUserService.selectAllUser(realname,loginname,identity,phone,address,page,limit);
        long count = pageInfo.getTotal();
        List<SysUserVO> result = new ArrayList<>();
        for (SysUser sysUser : pageInfo.getList()) {
            SysUserVO sysUserVO = new SysUserVO();
            BeanUtils.copyProperties(sysUser,sysUserVO);
            sysUserVO.setPwd("");
            result.add(sysUserVO);
        }
        return R.ok(count,result);
    }

    /**
     * 添加用户
     * @param sysUserDTO
     * @param bindingResult
     * @return
     */
    @PostMapping("/user/addUser")
    public R add(@Valid SysUserDTO sysUserDTO, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            String msg = bindingResult.getFieldError().getDefaultMessage();
            log.info("\"【添加用户信息】 参数不合法！ msg = {}\",msg");
            return R.error(HospitalExceptionEnums.REQUEST_PARAMETER_ERROR,msg);
        }
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserDTO,sysUser);
        sysUserService.add(sysUser);
        return R.ok("添加用户信息成功！");
    }

    /**
     * 修改用户
     * @param sysUserUpdateDTO
     * @param bindingResult
     * @return
     */
    @PostMapping("/user/updateUser")
    public R update(@Valid SysUserUpdateDTO sysUserUpdateDTO, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            String msg = bindingResult.getFieldError().getDefaultMessage();
            log.info("\"【修改用户信息】 参数不合法！ msg = {}\",msg");
            return R.error(HospitalExceptionEnums.REQUEST_PARAMETER_ERROR,msg);
        }
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserUpdateDTO,sysUser);
        sysUserService.update(sysUser);
        return R.ok("修改用户信息成功！");
    }

    /**
     * 展示角色分配信息
     * @param userid
     * @return
     */
    @GetMapping("/user/initUserRole")
    public R initUserRole(Integer userid){
        if(userid == null || userid <= 0){
            log.info("【分配角色信息】 参数不合法！");
            return R.error(HospitalExceptionEnums.REQUEST_PARAMETER_ERROR,"");
        }
        List<UserRoleVO> data = sysUserService.initUserRole(userid);
        return R.ok(data);
    }

    /**
     * 分配角色信息
     * @param userid
     * @param ids
     * @return
     */
    @PostMapping("/user/saveUserRole")
    public R saveUserRole(Integer userid,Integer[] ids){
        if(userid == null || userid <= 0){
            log.info("【分配角色信息】 参数不合法！");
            return R.error(HospitalExceptionEnums.REQUEST_PARAMETER_ERROR,"");
        }
        sysUserService.saveUserRole(userid,ids);
        return R.ok("分配角色信息成功！");
    }

    /**
     * 删除用户
     * @param userid
     * @return
     */
    @PostMapping("/user/deleteUser")
    public R deleteUser(Integer userid){
        if(userid == null || userid <= 0){
            log.info("【删除角色信息】 参数不合法！");
            return R.error(HospitalExceptionEnums.REQUEST_PARAMETER_ERROR,"");
        }
        sysUserService.deleteUser(userid);
        return R.ok("删除用户成功");
    }

    /**
     * 重置密码
     * @param userid
     * @return
     */
    @PostMapping("/user/resetUserPwd")
    public R resetUserPwd(Integer userid){
        if(userid == null || userid <= 0){
            log.info("【删除角色信息】 参数不合法！");
            return R.error(HospitalExceptionEnums.REQUEST_PARAMETER_ERROR,"");
        }
        sysUserService.resetUserPwd(userid);
        return R.ok("重置密码成功");
    }
}
